package com.yl.auth.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.model.entity.ExcelDictDTO;
import com.yl.auth.model.entity.Yuangong;
import com.yl.auth.model.vo.YexundizhirenyuanVo;
import com.yl.auth.model.vo.YuaGongPaiBanVo;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ln
 * @since 2023-06-15
 */
@Api(tags = "养老护理-夜巡管理")
@RestController
@RequestMapping("/admin/yexun")
public class YexunController {
    @Resource
    private YexunService yexunService;
    @Resource
    private YuangongService yuangongService;


    /**
     * 夜巡管理分页添加查询
     * @param page
     * @param limit
     * @param sname
     * @param sdizhiname
     * @param sinspectiontime
     * @param sinspectionstatus
     * @return
     */
    @GetMapping("/getyexun/{page}/{limit}")
    @ApiOperation(value = "获取全部夜巡信息")
    public Result getyexun(@PathVariable Integer page, @PathVariable Integer limit, @ApiParam("上报人条件查询")String sname, @ApiParam("地址条件查询")String sdizhiname, @ApiParam("巡检上报时间")String sinspectiontime, @ApiParam("巡检状态")Integer sinspectionstatus){
        Page<YexundizhirenyuanVo> iPage = new Page<>(page,limit);
        Page<YexundizhirenyuanVo> pageVo = yexunService.getPageVo(iPage, sname, sdizhiname, sinspectiontime, sinspectionstatus);
        return Result.ok(pageVo);
    }
    /**
     * 导出
     * @param response
     */
    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("yexun", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ExcelDictDTO.class).sheet("数据字典").doWrite(yexunService.listDictData());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

